package com.jspgou.cms.action.front;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.common.web.ResponseUtils;
import com.jspgou.common.web.session.SessionProvider;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.cms.entity.MemberCoupon;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.MemberCouponMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;


@Controller
public class MemberCouponAct {
	/**
	 * 会员中心优惠劵
	 */

	@RequestMapping(value = "/myCoupon.jspx", method = RequestMethod.GET)
	public String pay1(String cl,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		List<MemberCoupon> list=manage.getList(member.getId());
		model.addAttribute("couList", list);
		model.addAttribute("historyProductIds", "");
		model.addAttribute("cl", cl);
		Cookie[] cookie = request.getCookies();
		for(int i=0;i<cookie.length;i++){
			if(cookie[i].getName().equals("shop_record")){ 
				String str = cookie[i].getValue();
				model.addAttribute("historyProductIds", str);
				break;
			}
		}
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys("member", MessageResolver.getMessage(request,
				"tpl.myCoupon"), request);
	}
	

	@RequestMapping(value = "/useCoupon.jspx", method = RequestMethod.POST)//通过session存放优惠劵的优惠值( wang ze wu)
	public String useCoupon(String code,HttpServletRequest request, ModelMap model,HttpServletResponse response) {
		MemberCoupon bean=manage.getMemberCouponByCode(code);
		if(bean == null){
			ResponseUtils.renderJson(response, "验证码不对");
			return null;
		}
		if(bean.getIsuse()){
			ResponseUtils.renderJson(response, "已经用过了");
			return null;
		}
		if(bean.getCoupon().getCouponEndTime().getTime()<new Date().getTime()){
			ResponseUtils.renderJson(response, "已经过期了");
			return null;
		}
		bean.setIsuse(true);
		manage.update(bean);
	//	session.setAttribute(request, response, "couPrice", bean.getCoupon().getCouponPrice());
		BigDecimal bd=(BigDecimal)session.getAttribute(request, "couPrice");
		if(bd == null){
			session.setAttribute(request, response, "couPrice", bean.getCoupon().getCouponPrice());
			ResponseUtils.renderJson(response, bean.getCoupon().getCouponPrice()+"");
		}else{
			session.setAttribute(request, response, "couPrice", bean.getCoupon().getCouponPrice().add(bd));
			ResponseUtils.renderJson(response, bean.getCoupon().getCouponPrice().add(bd)+"");
		}
		return null;
	}
	@Autowired
	private MemberCouponMng manage;
	@Autowired
	private SessionProvider session;
}
